package com.makao.mtez.degrid;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.apache.hadoop.io.WritableComparable;
public class IntArray implements  WritableComparable<IntArray>{
	int number=6;
	Integer[] intValues=new Integer[number];
	 public IntArray() {  } 
	public void readFields(DataInput in) throws IOException {
		number=in.readInt();
		for(int ii=0;ii<number;ii++)
		{
			intValues[ii]=in.readInt();
		}
     }
	public void write(DataOutput out) throws IOException {
		out.writeInt(number);
		for(int ii=0;ii<number;ii++)
		{
			out.writeInt(intValues[ii]);
		}
	}
	public IntArray(int number, Integer[] intValues)
	{
		this.number=number;
		this.intValues=intValues;
	}
	
	 public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public Integer[] getIntValues() {
		return intValues;
	}
	public void setIntValues(Integer[] intValues) {
		this.intValues = intValues;
	}
	
	public IntArray(IntArray ia)
	{
		this.number=ia.getNumber();
		this.intValues=ia.getIntValues().clone();
	}
	
	@Override
		public int compareTo(IntArray comparor) {
			// TODO Auto-generated method stub
			int res = 0;
			Integer[] curvec = (Integer[])this.intValues;
			Integer[] cmpvec = (Integer[])comparor.intValues;
			
			for (int i = 0; i < curvec.length; i++) {
				int curres = (curvec[i]).compareTo(cmpvec[i]);
				if( curres == 0){
					continue;
				}
				else {
					return curres;
				}
			}
			return 0;
		}
	 
	  @Override
	    public boolean equals(Object o) {
	        if (this == o) return true;
	        if (o == null || getClass() != o.getClass()) return false;

	        IntArray ia = (IntArray) o;

	        Integer[] curvec = (Integer[])this.intValues;
			Integer[] cmpvec = (Integer[])ia.intValues;
			
			for (int i = 0; i < curvec.length; i++) {
				if(curvec[i].intValue()!=cmpvec[i].intValue()){
					return false;
				}
			}
			return true;

	    }

	    @Override
	    public int hashCode() {
	    	int hc = 0;
	    	for(Integer i:this.intValues){
	    		hc = hc + i.hashCode();
	    	}
	        return hc;
	    }
	    
	    @Override
	    public String toString() {
	    	StringBuilder sb = new StringBuilder();
	    	sb.append(this.number+" ");
	    	for(Integer i:this.intValues){
	    		sb.append(i+" ");
	    	}
	    	return sb.toString();
	    }
	    
	    public static void main(String[] args){
	    	Integer[] temp = new Integer[]{1,2,3,4,5,6};
	    	IntArray a1 = new IntArray(6, temp);
	    	IntArray a2 = new IntArray(6, temp);
	    	System.out.println(a1);
	    	System.out.println(a1.equals(a2));
	    	Set<IntArray> sets = new HashSet<IntArray>();
	    	sets.add(a1);sets.add(a2);
	    	Integer[] temp2 = new Integer[]{1,2,3,4,5,6};
	    	IntArray a3 = new IntArray(6, temp2);
	    	System.out.println(sets.contains(a1));
	    	System.out.println(sets.contains(a3));
	    }
}
